<?php

namespace app\admin\service;

use app\admin\traits\TableChangeTrait;
use app\common\model\AccessLog;
use app\common\model\Landing;
use app\common\model\Lawsuit;
use app\common\model\LogCaptcha;
use app\common\model\Member;
use app\common\model\Trade;
use ShidianUtil\Utils\TimeUtil;
use think\facade\Config;

/**
 * 渠道数据
 * Class PromotionTableService
 * @package app\admin\service
 */
class PromotionTableService
{
    private PromotionTable2Service $table2Service;

    public function __construct(PromotionTable2Service $table2Service)
    {
        $this->table2Service = $table2Service;
    }

    /**
     * 渠道数据统计
     * @param $groupId
     * @param array $chains
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public function index($groupId, array $chains, $startTime, $endTime)
    {

        $timeList = TimeUtil::timeList(TimeUtil::DAY, $startTime, $endTime, true);

        $days = [];
        foreach ($timeList as $item) {
            $s = $item['s_time'];
            $days[] = ['name' => date('j', $s) . '日'];
        }

        $fields = Config::get('promotion', []);

        $list = [];
        foreach ($fields as $field) {
            $list[] = [
                'name' => $field['title'],
                'datas' => $this->table2Service->indexOfGroupChains($groupId, $chains, $field['value'], $startTime, $endTime),
            ];
        }

        return [
            'day_count' => count($days),
            'days' => $days,
            'list' => $list,
        ];
    }
}
